package com.xidian.zhiancloud.util.tools;

import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;
import java.util.*;

public class StringUtil {

    public static boolean isNUllOrEmpty(String input) {
        if ((input == null) || (input.isEmpty())) {
            return true;
        }

        return false;
    }

    /**
     * 判断两个字符串是否有相同存在
     * @param str
     * @param str1
     * @return
     */
    public static boolean containRepeatChar(String str,String str1){
        try {
            String[] arr1 = str.split(",");
            String[] arr2 = str1.split(",");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < arr2.length; i++) {
                for (int j = 0; j < arr1.length; j++) {
                    if (arr1[j].equals(arr2[i])) {
                        sb.append(arr1[j] + ",");
                    }
                }
            }
            System.out.println("结果：" + sb.toString().substring(0, sb.toString().length() - 1));
            return true;
        }catch (Exception e){
            System.out.println("结果:没有重复的数据");
            return false;
        }
    }
    
    public static int StringtoInt(String value) {
        int nRet = -1;
        try {
            nRet = Integer.parseInt(value);
        } catch (NumberFormatException NFE) {
            NFE.getStackTrace();
        }

        return nRet;
    }

    public static String intToString(int value) {
        String strRet = Integer.toString(value);
        return strRet;
    }

    public static String getSqlValueString(Object value) {
        if (value == null) {
            return null;
        }

        String strTmp = value.toString();
        if (isNUllOrEmpty(strTmp)) {
            return null;
        }

        String strRet = "'" + value + "'";
        return strRet;
    }

    public static boolean stringToBoolean(String input) {
        if (isNUllOrEmpty(input))
            return false;

        boolean bRet = false;
        if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("false")) {
            bRet = Boolean.valueOf(input);
        }
        return bRet;
    }

    /**
     * 从 Unicode 形式的字符串转换成对应的编码的特殊字符串。 如 "\u9EC4" to "黄".
     * Converts encoded \\uxxxx to unicode chars
     * and changes special saved chars to their original forms
     *
     * @return 完成转换，返回编码前的特殊字符串。
     */
    public static String fromEncodedUnicode(String string) {
        /*
         * @param in
         *        Unicode编码的字符数组。
         * @param off
         *        转换的起始偏移量。
         * @param len
         *        转换的字符长度。
         * @param convtBuf
         *        转换的缓存字符数组。
         */
        char[] in = string.toCharArray();
        int off = 0;
        int len = string.length();


        char aChar;
        char[] out = new char[len]; // 只短不长
        int outLen = 0;
        int end = off + len;

        while (off < end) {
            aChar = in[off++];
            if (aChar == '\\') {
                aChar = in[off++];
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = in[off++];
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
                        }
                    }
                    out[outLen++] = (char) value;
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    }
                    out[outLen++] = aChar;
                }
            } else {
                out[outLen++] = (char) aChar;
            }
        }
        return new String(out, 0, outLen);
    }

    public static final String arrTest[] = {"[br]", "[/b]", "[/i]", "[/u]", "[/size]", "[/color]", "[/align]", "[/url]", "[/email]", "[/img]"};
    public static final String arrParam[] = {"\\[br\\]", "\\[b\\](.+?)\\[/b\\]",
            "\\[i\\](.+?)\\[/i\\]",
            "\\[u\\](.+?)\\[/u\\]",
            "\\[size=(.+?)\\](.+?)\\[/size\\]",
            "\\[color=(.+?)\\](.+?)\\[/color\\]",
            "\\[align=(.+?)\\](.+?)\\[/align\\]",
            "\\[url=(.+?)\\](.+?)\\[/url\\]",
            "\\[email=(.+?)\\](.+?)\\[/email\\]," +
                    "\\[img=(.+?)\\](.+?)\\[/img\\]"};
    public static final String arrCode[] = {"<br>", "<b>$1</b>", "<i>$1</i>", "<u>$1</u>",
            "<font size=\"$1\">$2</font>",
            "<font color=\"$1\">$2</font>",
            "<div align=\"$1\">$2</div>",
            "<a href=\"$1\" target=\"_blank\">$2</a>",
            "<a href=\"email:$1\">$2</a>",
            "<img src=\"$1\" border=0>$2</img>"};


    public static int getInt(String content) {
        int intContent;
        try {
            intContent = Integer.parseInt(content);
        } catch (Exception e) {
            intContent = 0;
        }
        return intContent;
    }

    public static long getLong(String content) {
        long lngContent;
        try {
            lngContent = Long.parseLong(content);
        } catch (Exception e) {
            lngContent = 0L;
        }
        return lngContent;
    }

    /**
     * @param str    原字符串
     * @param length 字符串达到多长才截取
     * @return
     */
    public static String subStringToPoint(String str, int length, String more) {

        String reStr = "";

        if (str.length() * 2 - 1 > length) {

            int reInt = 0;

            if (str == null)

                return "";

            char[] tempChar = str.toCharArray();

            for (int kk = 0; (kk < tempChar.length && length > reInt); kk++) {

                String s1 = String.valueOf(tempChar[kk]);

                byte[] b = s1.getBytes();

                reInt += b.length;

                reStr += tempChar[kk];

            }

            if (length == reInt || (length == reInt - 1)) {

                if (!reStr.equals(str)) {
                    reStr += more;
                }
            }

        } else {
            reStr = str;
        }
        return reStr;

    }

    public static String getURLEncoder(String content, String encode) {
        String strContent;
        try {
            strContent = java.net.URLEncoder.encode(content, encode);
        } catch (Exception e) {
            strContent = "";
        }
        return strContent;
    }

    public static String getURLDecoder(String content, String encode) {
        String strContent;
        try {
            strContent = java.net.URLDecoder.decode(content, encode);
        } catch (Exception e) {
            strContent = "";
        }
        return strContent;
    }


    /**
     * 将指定的对象转换为String类型
     *
     * @param curObject 传入对象参数
     * @return String
     */
    public static String getString(Object curObject) {
        if (null == curObject) {
            throw new NullPointerException("The input object is null.");
        } else {
            return curObject.toString();
        }
    }

    /**
     * 转换字符,用于替换提交的数据中存在非法数据:"'"
     *
     * @param Content
     * @return
     */
    public static String replaceChar(String content) {
        String newstr = "";
        newstr = content.replaceAll("\'", "''");
        return newstr;
    }

    /**
     * 对标题""转换为中文“”采用对应转换
     *
     * @param Content
     * @return
     */
    public static String replaceSymbol(String content) {
        int intPlaceNum = 0;
        int Num = 0;
        String strContent = content;
        while (true) {
            //判断是否还存在"
            intPlaceNum = strContent.indexOf("\"");
            if (intPlaceNum < 0) {
                break;
            } else {
                if (Num % 2 == 0) {
                    strContent = strContent.replaceFirst("\"", "“");
                } else {
                    strContent = strContent.replaceFirst("\"", "”");
                }
                Num = Num + 1;
            }
        }
        return strContent;
    }

    /**
     * 替换HTML标记
     *
     * @param Content
     * @return
     */
    public static String replaceCharToHtml(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("<", "&lt;");
        strContent = strContent.replaceAll(">", "&gt;");
        strContent = strContent.replaceAll("\"", "&quot;");
        return strContent;
    }

    public static String replaceHtmlToChar(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("&lt;", "<");
        strContent = strContent.replaceAll("&gt;", ">");
        strContent = strContent.replaceAll("&quot;", "\"");
        return strContent;
    }

    //数据库替换
    public static String replaceCharToSql(String content) {
        String strContent = content;
        strContent = strContent.replaceAll("%", "\\\\%");
        return strContent;
    }

    public static String toHtmlValue(String value) {
        if (null == value) {
            return null;
        }
        char a = 0;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < value.length(); i++) {
            a = value.charAt(i);
            switch (a) {
                // 双引号
                case 34:
                    buf.append("&#034;");
                    break;
                // &号
                case 38:
                    buf.append("&amp;");
                    break;
                // 单引号
                case 39:
                    buf.append("&#039;");
                    break;
                // 小于号
                case 60:
                    buf.append("&lt;");
                    break;
                // 大于号
                case 62:
                    buf.append("&gt;");
                    break;
                default:
                    buf.append(a);
                    break;
            }
        }
        return buf.toString();
    }


    /**
     * 标题中含有特殊字符替换 如:●▲@◎※ 主要在标题中使用
     *
     * @param Content
     * @return
     */
    public static String replaceSign(String content) {
        String strContent = "";
        strContent = content.replaceAll("\\*", "");
        strContent = strContent.replaceAll("\\$", "");
        strContent = strContent.replaceAll("\\+", "");
        String arrStr[] = {":", "：", "●", "▲", "■", "@", "＠",
                "◎", "★", "※", "＃", "〓", "＼", "§", "☆",
                "○", "◇", "◆", "□", "△", "＆", "＾", "￣",
                "＿", "♂", "♀", "Ю", "┭", "①", "「", "」", "≮", "§",
                "￡", "∑", "『", "』", "⊙", "∷", "Θ", "の", "↓", "↑",
                "Ф", "~", "Ⅱ", "∈", "┣", "┫", "╋", "┇", "┋", "→",
                "←", "!", "Ж", "#"};
        for (int i = 0; i < arrStr.length; i++) {
            if ((strContent.indexOf(arrStr[i])) >= 0) {
                strContent = strContent.replaceAll(arrStr[i], "");
            }
        }

        return strContent;
    }

    /**
     * 替换所有英文字母
     *
     * @param Content
     * @return
     */
    public static String replaceLetter(String content) {
        String strMark = "[^[A-Za-z]+$]";
        String strContent = "";
        strContent = content.replaceAll(strMark, "");
        return strContent;
    }

    /**
     * 替换所有数字
     *
     * @param Content
     * @return
     */
    public static String replaceNumber(String content) {
        String strMark = "[^[0-9]+$]";
        String strContent = "";
        strContent = content.replaceAll(strMark, "");
        return strContent;
    }

    /**
     * 将/n转换成为回车<br> ,空格转为&nbsp;
     *
     * @param Content
     * @return
     */
    public static String replaceBr(String content) {
        if (content == null) {
            return "";
        }
        String strContent = "";

        // String strMark ="[/\n\r\t]";

        //strContent = content.replaceAll(strMark,"<br>");

        strContent = content.replaceAll("\n\r\t", "<br>");
        strContent = strContent.replaceAll("\n\r", "<br>");
        strContent = strContent.replaceAll("\r\n", "<br>");
        strContent = strContent.replaceAll("\n", "<br>");
        strContent = strContent.replaceAll("\r", "<br>");
        strContent = strContent.replaceAll(" ", "&nbsp;");
        return strContent;
    }

    /**
     * 清除所有<>标记符号 主要在搜索中显示文字内容 而不显示样式
     *
     * @param Content
     * @return
     */
    public static String replaceMark(String content) {
        String strContent = "";
        String strMark = "<\\s*[^>]*>";
        strContent = content.trim();
        strContent = strContent.replaceAll("\"", "");
        strContent = strContent.replaceAll("\'", "");
        //删除所有<>标记
        strContent = strContent.replaceAll(strMark, "");
        strContent = strContent.replaceAll("&nbsp;", "");
        strContent = strContent.replaceAll(" ", "");
        strContent = strContent.replaceAll("　", "");
        strContent = strContent.replaceAll("\r", "");
        strContent = strContent.replaceAll("\n", "");
        strContent = strContent.replaceAll("\r\n", "");
        return strContent;
    }

    /**
     * 清楚WOrd垃圾代码
     *
     * @param Content
     * @return
     */
    public static String clearWord(String content) {
        String strContent = "";
        strContent = content.trim();
        strContent = strContent.replaceAll("x:str", "");
        //Remove Style attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) style=\"([^\"]*)\"", "<$1");
        //Remove all SPAN  tags
        strContent = strContent.replaceAll("<\\/?SPAN[^>]*>", "");
        //Remove Lang attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) lang=([^ |>]*)([^>]*)", "<$1$3");
        //Remove Class attributes
        strContent = strContent.replaceAll("<(\\w[^>]*) class=([^ |>]*)([^>]*)", "<$1$3");
        //Remove XML elements and declarations
        strContent = strContent.replaceAll("<\\\\?\\?xml[^>]*>", "");
        //Remove Tags with XML namespace declarations: <o:p></o:p>
        strContent = strContent.replaceAll("<\\/?\\w+:[^>]*>", "");
        return strContent;
    }

    /**
     * 对组ID信息进行处理 转换为标准ID组 并过滤重复的信息
     *
     * @param teamId
     * @return
     */
    public static String checkTeamId(String teamId) {
        String strTeamId = "";
        String strTempId = "";
        String strTemp = "";
        String[] arrTeamId = teamId.split(",");
        for (int num = 0; num < arrTeamId.length; num++) {
            strTemp = arrTeamId[num].trim();
            if ((!strTemp.equals("")) && (!strTemp.equals("0"))) {
                if ((strTempId.indexOf("," + strTemp + ",")) >= 0) { //表示已经保存过了
                } else {
                    if (strTeamId.equals("")) {
                        strTeamId = strTemp;
                        strTempId = strTempId + "," + strTemp + ",";
                        ;
                    } else {
                        strTeamId = strTeamId + "," + strTemp;
                        strTempId = strTempId + strTemp + ",";
                    }
                }
            }
        }
        return strTeamId;
    }


    public static String replaceUbb(String content) {
        String strContent = content;
        try {
            for (int num = 0; num < arrTest.length; num++) {
                if ((strContent.indexOf(arrTest[num])) >= 0) {
                    try {
                        strContent = strContent.replaceAll(arrParam[num], arrCode[num]);
                    } catch (Exception ex) {
                    }
                }
            }
        } catch (Exception e) {
            //System.out.println("UBB CODE 错误"+e);
        }
        return strContent;
    }


    /**
     * 判断传入的字符串如果为null则返回"",否则返回其本身
     *
     * @param string
     * @param instant
     * @return String
     */
    public static String convertNull(String string, String instant) {
        return isNull(string) ? instant : string;
    }

    /**
     * {@link #convertNull(String, String)}
     *
     * @param string
     * @return String
     */
    public static String convertNull(String string) {
        return convertNull(string, "");
    }

    /**
     * 判断对象是否为空
     *
     * @param obj Object
     * @return boolean 空返回true,非空返回false
     */
    public static boolean isNull(Object obj) {
        return (null == obj) ? true : false;
    }

    /**
     * Description:判断字段空null <br>
     *
     * @param s
     * @return boolean
     */
    public static boolean isNull(String s) {
        if (s == null || "".equals(s.trim())) {
            return true;
        }

        return false;
    }

    /**
     * 获取百分比
     *
     * @param p1
     * @param p2
     * @return
     */
    public static String percent(double p1, double p2) {
        if (p2 == 0) {
            return "0.00%";
        }
        String str;
        double p3 = p1 / p2;
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(2);
        str = nf.format(p3);
        return str;
    }

    /**
     * 字符串编码转换的实现方法
     *
     * @param str        待转换编码的字符串
     * @param oldCharset 原编码
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String oldCharset, String newCharset) {
        try {
            if (str != null) {
                //用旧的字符编码解码字符串。解码可能会出现异常。
                byte[] bs = str.getBytes(oldCharset);
                //用新的字符编码生成字符串
                return new String(bs, newCharset);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }

    /**
     * 字符串编码转换的实现方法
     *
     * @param str        待转换编码的字符串
     * @param newCharset 目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public String changeCharset(String str, String newCharset) {
        try {
            if (str != null) {
                //用默认字符编码解码字符串。
                byte[] bs = str.getBytes();
                //用新的字符编码生成字符串
                return new String(bs, newCharset);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 解析html中的参数信息
     *
     * @param elementStr
     * @return
     */
    public static Map<String, String> getConfigValue(String elementStr) {
        try {
            elementStr = java.net.URLDecoder.decode(elementStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int start = elementStr.indexOf("configvalue");
        Map<String, String> map = null; //参数的键值对
        if (start != -1) {
            map = new HashMap<String, String>();
            start = elementStr.indexOf("\"", start);
            int end = elementStr.lastIndexOf("||");
            if (start < 0 || end < 0) {
                return null;
            }
            String configValue = elementStr.substring(start + 1, end);
            String[] values = configValue.split("\\|\\|");

            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                if (value != null && value.trim().length() > 1) {
                    int de = value.indexOf("=");
                    if (de > 0) {
                        String name = value.substring(0, de);
                        String v = value.substring(de + 1);
                        map.put(name, v);
                    }
                }
            }
        }
        return map;
    }

    public static String getUUID() {
        final String s = UUID.randomUUID().toString();
        final StringBuilder sb = new StringBuilder(32);
        sb.append(s.substring(0, 8)).append(s.substring(9, 13)).append(s.substring(14, 18))
                .append(s.substring(19, 23)).append(s.substring(24));
        return sb.toString();
    }

    public static String getUIDNumber(String prefix) {
        if (prefix == null) {
            prefix = "";
        }
        return prefix + System.currentTimeMillis()
                + StringUtil.getNumber6FromMath();
    }

    /**
     * 产生随机6位数-math
     *
     * @return
     */
    public static String getNumber6FromMath() {

        // Double d = Math.random();
        Long xx = Math.round(Math.random() * 1000000);
        while (xx < 100000) {
            xx = Math.round(Math.random() * 1000000);
        }
        return String.valueOf(xx);

    }

    /**
     * 产生随机6位数-Random
     *
     * @return
     */
    public static String getNumber6FromRandom() {
        Random r = new Random();
        int xx = r.nextInt(1000000);
        while (xx < 100000) {
            xx = r.nextInt(1000000);
        }
        return String.valueOf(xx);
    }

    /**
     * 转换空值为0
     *
     * @param str
     * @return
     */
    public static String conventString(String str) {
        return null == str || "".equals(str) ? "" + "0" : str;
    }

    /**
     * 验证字符串是否是空串
     *
     * @param valString
     * @return
     */
    public static boolean isEmptyString(String valString) {
        if (valString == null) {
            return true;
        } else if (valString.trim().equalsIgnoreCase("")
                || valString.trim().length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否为空
     *
     * @param str
     * @return
     */
    public static boolean isNullOrBlank(String str) {
        return (str == null || str.trim().length() == 0);
    }

    /**
     * 判断属否为Integer
     *
     * @param inputStr
     * @return
     */
    public static boolean isIntegerValue(String inputStr) {
        try {
            new Integer(inputStr);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 判断属否为Long
     *
     * @param inputStr
     * @return
     */
    public static boolean isLongValue(String inputStr) {
        try {
            new Long(inputStr);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 把字符串按照给定的切割符截取成字符串数组
     *
     * @param Source
     * @param Desizeer
     * @return
     */
    public static String[] SplitString(String Source, String Desizeer) {
        if ("null".equalsIgnoreCase(Source) || Source == null) {
            return new String[]{""};
        }
        int iCount, iPos, iLength;
        boolean bEnd; // 判断结束的符号是不是分割符号
        String sTemp; //
        String[] aSplit = null, t = null; // aSplit结果返回 t临时的

        sTemp = Source;
        iCount = 0;
        iLength = Desizeer.length();
        bEnd = sTemp.endsWith(Desizeer);

        for (; ; ) {
            iPos = sTemp.indexOf(Desizeer);
            if (iPos < 0) // 直到没有分割的字符串，就退出
                break;
            else {

                if (iCount > 0)
                    t = aSplit; // 第一次，不用拷贝数组

                iCount++;
                aSplit = new String[iCount]; // 新的数组，

                if (iCount > 1) { // 不是第一次，拷贝数组
                    for (int i = 0; i < t.length; i++)
                        aSplit[i] = t[i];
                }

                aSplit[iCount - 1] = sTemp.substring(0, iPos);
                sTemp = sTemp.substring(iPos + iLength); // 取余下的字符串
            }
        }

        if ((sTemp.length() >= 0) || bEnd) { // 判断最后剩余的 String，如果最后的字符是分割符号
            if (iCount > 0)
                t = aSplit;
            iCount++;
            aSplit = new String[iCount];
            if (iCount > 1) {
                for (int i = 0; i < t.length; i++)
                    aSplit[i] = t[i];
            }

            aSplit[iCount - 1] = sTemp;
        }

        return aSplit;
    }

    /**
     * 字符串数组转指定分隔符的字符串
     *
     * @param arrString
     * @param Desizeer
     * @return
     */
    public static String ArrayToString(String[] arrString, String Desizeer) {
        String resultStr = "";
        if (arrString == null || arrString.length == 0) {
            return resultStr;
        } else {
            for (int i = 0; i < arrString.length; i++) {
                if (i == 0) {
                    resultStr += arrString[i];
                } else {
                    resultStr += Desizeer;
                    resultStr += arrString[i];
                }
            }
        }
        return resultStr;
    }

    /**
     * obj to 字符串
     *
     * @param obj
     * @param defaultValue
     * @return
     */
    public static String objectToString(Object obj, String defaultValue) {
        String result = defaultValue;
        if (obj != null) {
            if (!isEmptyString(obj.toString())) {
                result = obj.toString();
            }
        }
        return result;

    }

    /**
     * 将数组array用字符串str连接
     *
     * @param str   连接符
     * @param array 连接的数组
     * @return
     */
    public static String implode(String str, Object[] array) {
        if (str == null || array == null)
            return "";
        String result = "";
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                result += array[i].toString();
            } else {
                result += array[i].toString() + str;
            }
        }
        return result;
    }

    public static String implodeValue(String str, Object[] array) {
        if (str == null || array == null)
            return "";
        String result = "";
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                result += "?";
            } else {
                result += "?" + str;
            }
        }
        return result;
    }

    public static String addCDATA(String str) {
        return "<![CDATA[" + str + "]]>";
    }

    /**
     * JSON字符串特殊字符处理，比如：“\A1;1300”
     *
     * @param s
     * @return String
     */
    public static String string2Json(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '\"':
                    sb.append("\\\"");
                    break;
                case '\\':
                    sb.append("\\\\");
                    break;
                case '/':
                    sb.append("\\/");
                    break;
                case '\b':
                    sb.append("\\b");
                    break;
                case '\f':
                    sb.append("\\f");
                    break;
                case '\n':
                    sb.append("\\n");
                    break;
                case '\r':
                    sb.append("\\r");
                    break;
                case '\t':
                    sb.append("\\t");
                    break;
                default:
                    sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            String ppp = getUUID();
            System.out.println(ppp);
        }
        int a = 0;
        System.out.println(addCDATA(""));
    }


    public static List<String> subbylength(String str, int length) {

        List<String> list = new ArrayList<String>();
        String s = null;
        int t = 0;
        int a = str.length() / length;
        for (int i = 0; i < a; i++) {
            if (str.length() > 0) {
                t = str.length() > length ? length : str.length();
                s = str.substring(0, t);
                str = str.substring(t);
            }
            list.add(s);
        }
        return list;
    }

    /**
     * @param parentId
     * @param maxId
     * @return
     */
    public static String addOne(String parentId, String maxId) {
        if ("root".equals(parentId)) {
            parentId = "";
        }
        if ("0".equals(parentId)) {
            parentId = "";
        }
        if (isNullOrEmpty(maxId)) {
            return parentId + "01";
        }

        maxId = maxId.substring(maxId.length() - 2, maxId.length());

        int result = Integer.parseInt(maxId) + 1;

        if (result < 10) {
            return parentId + "0" + result;
        } else {
            return parentId + result + "";
        }
    }

    /**
     * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
     *
     * @param obj
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null)
            return true;

        if (obj instanceof CharSequence)
            return ((CharSequence) obj).length() == 0;

        if (obj instanceof Collection)
            return ((Collection) obj).isEmpty();

        if (obj instanceof Map)
            return ((Map) obj).isEmpty();

        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNullOrEmpty(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

}
